package com.weisy.signin.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.weisy.signin.R;

import androidx.annotation.NonNull;

/**
 * @author weisy
 * @function 通用消息对话框
 * @date 2018/11/26 15:55
 */
public class MsgDialog extends Dialog {

    private static final String TAG = "MsgDialog";
    public static final int TYPE_MSG = 1;
    public static final int TYPE_VERIFY = 2;

    public MsgDialog(@NonNull Context context) {
        super(context, R.style.styleDialog);
    }

    public static class Builder {
        private Context context;

        private String mTitle;
        private String msg;
        private String txtLeft;
        private String txtRight;
        private float btnSize = 16f;
        private float titleSize = 18f;
        private float msgSize = 18f;
        private int btnColor = -1;
        private int msgColor = -1;
        private boolean isOutTouchCancel = false;
        private boolean showCloseBtn = false;
        private boolean isCenter=true;
        private DialogInterface.OnClickListener leftOnClickListener;
        private DialogInterface.OnClickListener rightOnClickListener;
        private OnVerifyResultListener onVerifyResultListener;
        private OnVerifyOnClickListener onVerifyOnClickListener;
        private int type = 1;
        private int verifyColor = -1;
        private String verifyMsg;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setmTitle(String title) {
            this.mTitle = title;
            return this;
        }

        public Builder setMsg(String msg) {
            this.msg = msg;
            return this;
        }

        public Builder setCenter(boolean center) {
            isCenter = center;
            return this;
        }

        public Builder setTxtLeft(String txtLeft, DialogInterface.OnClickListener leftOnClickListener) {
            this.txtLeft = txtLeft;
            this.leftOnClickListener = leftOnClickListener;
            return this;
        }

        public Builder setTxtRight(String txtRight, DialogInterface.OnClickListener rightOnClickListener) {
            this.txtRight = txtRight;
            this.rightOnClickListener = rightOnClickListener;
            return this;
        }

        public Builder setTxtRight(String txtRight, OnVerifyResultListener onVerifyResultListener) {
            this.txtRight = txtRight;
            this.onVerifyResultListener = onVerifyResultListener;
            return this;
        }

        public Builder setBtnSize(float btnSize) {
            this.btnSize = btnSize;
            return this;
        }

        public Builder setTitleSize(float titleSize) {
            this.titleSize = titleSize;
            return this;
        }

        public Builder setMsgSize(float msgSize) {
            this.msgSize = msgSize;
            return this;
        }

        public Builder setBtnColor(int btnColor) {
            this.btnColor = btnColor;
            return this;
        }

        public Builder setMsgColor(int msgColor) {
            this.msgColor = btnColor;
            return this;
        }

        public Builder setOutTouchCancel(boolean outTouchCancel) {
            isOutTouchCancel = outTouchCancel;
            return this;
        }

        public Builder setShowCloseBtn(boolean showCloseBtn) {
            this.showCloseBtn = showCloseBtn;
            return this;
        }

        public Builder setType(int type) {
            this.type = type;
            return this;
        }

        public Builder setVerifyColor(int verifyColor) {
            this.verifyColor = verifyColor;
            return this;
        }

        public Builder setVerifyMsg(String verifyMsg) {
            this.verifyMsg = verifyMsg;
            return this;
        }

        public Builder setVerifyOnClickListener(OnVerifyOnClickListener onVerifyOnClickListener) {
            this.onVerifyOnClickListener = onVerifyOnClickListener;
            return this;
        }

        public MsgDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (null == inflater) {
                Log.e(TAG, "dialog create is fail,inflater is null");
                return null;
            }
            return createMsgDialog(inflater.inflate(R.layout.layout_dialog_msg, null));
        }

        private MsgDialog createMsgDialog(View layout) {
            final MsgDialog msgDialog = new MsgDialog(context);
            msgDialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            msgDialog.setCanceledOnTouchOutside(isOutTouchCancel);
            if (type == MsgDialog.TYPE_VERIFY) {
                layout.findViewById(R.id.real_dialog_verify).setVisibility(View.VISIBLE);
                layout.findViewById(R.id.txt_dialog_msg).setVisibility(View.GONE);
            } else {
                layout.findViewById(R.id.real_dialog_verify).setVisibility(View.GONE);
                layout.findViewById(R.id.txt_dialog_msg).setVisibility(View.VISIBLE);
            }
            if (isCenter){
                ((TextView)layout.findViewById(R.id.txt_dialog_msg)).setGravity(Gravity.CENTER);
            }
            Button btnTitle = layout.findViewById(R.id.txt_dialog_title);
            TextView txtMsg = layout.findViewById(R.id.txt_dialog_msg);
            TextView txtVerifyMsg = layout.findViewById(R.id.txt_dialog_verify_msg);
            final TextView txtVerify = layout.findViewById(R.id.txt_dialog_get);
            final EditText editVerify = layout.findViewById(R.id.edit_dialog_code);

            Button btnViewLeft = layout.findViewById(R.id.txt_dialog_left);
            Button btnViewRight = layout.findViewById(R.id.txt_dialog_right);
            ImageView imgClose = layout.findViewById(R.id.txt_dg_close);
            View viewLine = layout.findViewById(R.id.view_dialog_mid);
            if (showCloseBtn) {
                imgClose.setVisibility(View.VISIBLE);
                imgClose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (msgDialog.isShowing())
                            msgDialog.dismiss();
                    }
                });
            } else {
                imgClose.setVisibility(View.GONE);
            }

            if (TextUtils.isEmpty(mTitle)) {
                btnTitle.setVisibility(View.GONE);
            } else {
                btnTitle.setVisibility(View.VISIBLE);
                btnTitle.setText(mTitle);
            }
            if (!TextUtils.isEmpty(verifyMsg)) {
                txtVerifyMsg.setText(verifyMsg);
                txtVerifyMsg.setVisibility(View.VISIBLE);
            } else {
                txtVerifyMsg.setVisibility(View.GONE);
            }
            txtMsg.setText(msg);
            btnTitle.setTextSize(TypedValue.COMPLEX_UNIT_DIP, titleSize);
            txtMsg.setTextSize(TypedValue.COMPLEX_UNIT_DIP, msgSize);
            if (msgColor != -1) {
                txtMsg.setTextColor(msgColor);
            }
            if (btnColor != -1) {
                btnViewLeft.setTextColor(btnColor);
                btnViewRight.setTextColor(btnColor);
            }
            if (verifyColor != -1) {
                txtVerify.setTextColor(verifyColor);
            }
            btnViewLeft.setTextSize(TypedValue.COMPLEX_UNIT_DIP, btnSize);
            btnViewRight.setTextSize(TypedValue.COMPLEX_UNIT_DIP, btnSize);
            if (TextUtils.isEmpty(txtLeft)) {
                btnViewLeft.setVisibility(View.GONE);
                viewLine.setVisibility(View.GONE);
            } else {
                btnViewLeft.setVisibility(View.VISIBLE);
                btnViewLeft.setText(txtLeft);
                btnViewLeft.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (null != leftOnClickListener) {
                            leftOnClickListener.onClick(msgDialog, DialogInterface.BUTTON_NEGATIVE);
                        }
                    }
                });
            }
            if (TextUtils.isEmpty(txtRight)) {
                btnViewRight.setVisibility(View.GONE);
                viewLine.setVisibility(View.GONE);
            } else {
                btnViewRight.setVisibility(View.VISIBLE);
                btnViewRight.setText(txtRight);
                btnViewRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (null != rightOnClickListener) {
                            rightOnClickListener.onClick(msgDialog, DialogInterface.BUTTON_POSITIVE);
                        }
                        if (null != onVerifyResultListener) {
                            onVerifyResultListener.onResult(msgDialog, editVerify.getText().toString().trim());
                        }
                    }
                });
            }
            final CountDownTimer timer = new CountDownTimer(60 * 1000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    txtVerify.setText(String.format("%s后重新发送", millisUntilFinished / 1000));
                    txtVerify.setText(String.format("%s后重新发送", millisUntilFinished / 1000));
                }

                @Override
                public void onFinish() {
                    txtVerify.setEnabled(true);
                    txtVerify.setText("重新发送");
                }
            };
            txtVerify.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != onVerifyOnClickListener) {
                        onVerifyOnClickListener.onClick(msgDialog, timer);
                    }
                    txtVerify.setEnabled(false);
                }
            });
            msgDialog.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    timer.cancel();
                }
            });
            return msgDialog;
        }

    }

    public interface OnVerifyResultListener {
        void onResult(DialogInterface dialog, String verify);
    }

    public interface OnVerifyOnClickListener {
        void onClick(DialogInterface dialog, CountDownTimer timer);
    }
}
